package com.example.easydownloadback.interceptor;

import com.example.easydownloadback.exception.ServiceException;
import com.example.easydownloadback.util.Code;
import com.example.easydownloadback.util.UserHolder;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //如果是OPTIONS请求 直接放行
        String method = request.getMethod();
        if(method.equals("OPTIONS")){
            return true;
        }
        // 1.判断是否需要去拦截
        if (UserHolder.getUser() == null) {
            throw new ServiceException(Code.power_error, "未登录授权！");
        }
        // 2.有用户，放行
        return true;
    }
}
